<?php
class UsersController extends AppController {

	var $name = 'Users';

	function index() {
		$this->User->recursive = 0;
		$this->set('users', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid User.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('users', $this->User->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->User->create();
			if ($this->User->save($this->data)) {
				$this->Session->setFlash(__('The User has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The User could not be saved. Please, try again.', true));
			}
		}
		$groups = $this->User->Group->find('list',array(
			'conditions' => '', 'fields' => array('Group.id', 'Group.name')));
		$this->set(compact('groups'));

		$coaches = $this->User->Coach->find('list',array(
			'conditions' => '', 'fields' => array('Coach.id', 'Coach.strName')));
		$this->set(compact('coaches'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid User', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->User->save($this->data)) {
				$this->Session->setFlash(__('The User has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The User could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->User->read(null, $id);
		}
		$groups = $this->User->Group->find('list',array(
			'conditions' => '', 'fields' => array('Group.id', 'Group.name')));
		$this->set(compact('groups'));

		$coaches = $this->User->Coach->find('list',array(
			'conditions' => '', 'fields' => array('Coach.id', 'Coach.strName')));
		$this->set(compact('coaches'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for User', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->User->delete($id)) {
			$this->Session->setFlash(__('User deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}

	function login() {
		$user = $this->Session->read('Auth.User');
		if ($user)
		{
			if (!empty($this->data)) {
				if( $this->data['User']['remember_me'] == 1) {
					$cookie = array();
					$cookie['username'] = $this->data['User']['username'];
					$cookie['password'] = $this->data['User']['password'];
					$this->Cookie->write('Auth.User', $cookie, true, '+2 weeks');
					unset($this->data['User']['remember_me']);
				}
			}
			$this->Session->setFlash('Welcome '.$user['username'].'!');
			$this->redirect($this->Auth->redirect());
		}

		if (empty($this->data)) {
			$cookie = $this->Cookie->read('Auth.User');
			if (!is_null($cookie)) {
				if ($this->Auth->login($cookie)) {
					//  Clear auth message, just in case we use it.
					$this->Session->del('Message.auth');
					$this->redirect($this->Auth->redirect());
				}
			}
		}
	}

	function logout() {
		$this->Session->setFlash('Good-Bye');
		$this->redirect($this->Auth->logout());
	}

	function register() {
		if ($this->data) {
			if ($this->data['User']['password'] == $this->Auth->password($this->data['User']['password_confirm'])) {
				$this->User->create();
				$this->User->save($this->data);
			}
		}
	}

	/** Initialize the ACL */
	/*function init_db() {
		$group =& $this->User->Group;
		$group->id = 1;
		$this->Acl->allow($group, 'controllers');
	}*/
}
?>